import router from "@/router/index.js";
import {getToken} from "@/composables/cookie.js";
import {hidePageLoading, showMessage, showPageLoading} from "@/composables/util.js";


router.beforeEach((to, from, next) => {
    console.log('==> 全局路由前置守卫')
    // 展示页面加载 Loading
    showPageLoading()
    // 若用户未登录，则跳转登录页
    let token = getToken();
    if(!token && to.path.startsWith('/admin')) {
        showMessage('请先登录', 'warning' )
        next({path:'/login'})
    }else if(token && to.path === '/login'){
        showMessage('请勿重复登录', 'warning')
        next({path:'/admin/index'})
    }else{
        next()
    }
})

// 全局路由后置守卫
router.afterEach((to, from) => {
    let title = (to.meta.title?to.meta.title:'') + ' - Weblog'
    document.title = title

    // 隐藏页面加载 Loading
    hidePageLoading()
})